
var extC_Debug = false;
/**
 *ExtChain helper construct thing
 *
 *Naming conventions:
 *      All ExtJS expected Object keys will be defined as used
 *      All Helpers must be SnakeCased ( ex AddItem )
 *      All specific vals will be CAPITALIZED ( ex region: north == NORTH() )
 *
 *      I haven't been to good with this one yet, but helper arguments should be of the following format
 *      integers: num
 *      text input: str
 *      boolean flags: state and default to true if no argument given
 *
 *
 */

function ExtChain(starter) {
        //To try and enforce some sort of cleanliness, I'm hiding obj in scope        
        var obj = (typeof starter  == "object") ? starter : {};
                
        //TODO - should I short-circuit here and return a new instance if called as a function?
        
        //chains are ordered alphabetically
        //A's
        //B's
        //C's
        this.checked = function (state) { obj.checked = state || true ; return this; };
        this.closable = function (state) {obj.closable = state || true ; return this; };
        this.collapsible = function () { obj.collapsible = arguments[0] || true; return this; }
        //D's
        this.draggable = function (state) { obj.draggable = state || true ; return this; };
        //E's
        //F's
        //G's
        //H's
        this.height = function (num) { obj.height = num; return this;};
        this.html = function (str) { obj.html = str; return this; };
        //I's
        this.items = function (items) { obj.items = items; if( extC_Debug === true && ! ( obj.items instanceof "array")) { throw new Exception("items should be an array!"); }   return this; };
        //J's
        //K
        //L
        this.layout = function(str) { obj.layout = str; return this; };
        //M's
        this.maxSize = function(num) { obj.maxSize = num; return this; }
        this.minimizable = function (state) { obj.minimizable = state || true; return this;};
        this.minSize = function(num) { obj.minSize = num; return this; }
        
        //N
        //O
        //P
        //Q        
        //R's
        this.region = function(str) { obj.region = str; return this; }
        this.resizable = function (state) { obj.resizable = state || true; return this; };
        //S
        //T's
        this.text = function (str) { obj.text = str; return this; };
        this.title = function(str) { obj.title = str; return this; };
        //U
        //V
        //W's        
        this.width = function (num) { obj.width = num; return this;};
        //X
        //Y
        //Z
        
        //Helper's and shortcuts go here
        
        //a
        this.AddItem = function (item) {
                if( item instanceof ExtChain){
                        item = item.p();
                }
                
                if(obj.items === undefined){
                        obj.items = [item];
                } else {
                        obj.items.push(item);
                }
                
                return this;
            };
        //b
        this.Behaviors = function (myMinimizable, myDraggable, myClosable) {
                if(myMinimizable !== null) { obj.minimizable = myMinimizable; }
                if(myDraggable !== null) { obj.draggable     = myDraggable; }
                if(myClosable !== null) { obj.closable       = myClosable; }
                return this;
        };
        //c
        this.CENTER = function() { obj.region = "center"; return this; }
        //d
        //e
        this.EAST = function() { obj.region = "east"; return this;}
        //f
        //g
        //h
        this.HW = function (myHeight,myWidth) {
                obj.height = myHeight;
                obj.width = myWidth;
                return this;
        };
        //i
        //j
        //k
        //l
        this.log = function(){
                if(typeof console == "object" && typeof console.log == "function"){
                        console.log(arguments);
                }else{
                        alert(arguments)
                }
                return this; //Technically you should never chain log, but I've seen weirder things in life
        }
        //m
        this.Merge = function (inject) {
                if(inject instanceof ExtChain){
                        inject = inject.p();
                }
                for (var p in inject) {
                        //Unfortunately doesn't seem right, keeps picking up stuff I really don't want :()
                        if(inject.hasOwnProperty(p)){
                                obj[p] = inject[p];        
                        }                       
                }
                return this;
        };
        //n
        this.NORTH = function() { obj.region = "north"; return this;}
        //o
        //p        
        this.p = function () {
           return obj;
        };
        //q
        //r
        //s
        this.SOUTH = function(){ obj.region = "south"; return this; }
        //t
        //u
        //v
        //w
        this.WEST = function() { obj.region = "west"; return this; }
        //x
        //y
        //z
        
     
        //FireFox specific helper, this should CAUSE 0 problems because it only works with FF2+
        this.__noSuchMethod__ = function(item, args){
                throw("Unhandled chain " + item + "(" + args.join(", ") + ")");
                //this.log("Unhandled chain " + item, args);
                return this;
        }
        
        //I love this construct, no more weird errors because I forgot new
        //Undoubtably someone is going to argue this is bad form.
        return ( this instanceof ExtChain )
            ?  this.ExtChain  
            :  new ExtChain(obj);
     
    };
    
//shortcut
$EC = ExtChain;
  
        
    /*Before
    var z = new Ext.Window({
        html: "Hello World",
        closable: true,
        width: 300, height: 300
        minimizable: false,
        draggable: false
    });
    
    Revision 2
    myChain = ExtChain()
                            .html("hello World!")
                           .closable(true).width(300)
                           .height(100).minimizable(false)
                           .resizable(false).draggable(false).p();
                           
    var w = new Ext.Window(myChain);
    w.show();
    */
    //Revision 2+
    //var myChain = ExtChain().html("Hello world").hw(100,300).behaviors(false,false,false).$();
    
    
